package com.hmyuan.controller.back;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Category;
import com.hmyuan.service.CategoryService;
import com.hmyuan.util.PageUtil;
import com.hmyuan.util.UserUtil;

/**
 * 类别controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/category")
public class CategoryController {

	@Resource
	CategoryService categoryService;
	@Resource
	PageUtil pageUtil;
	/**
	 * 类别管理页面
	 */
	@RequestMapping("/categoryPage")
	public String categoryPage(HttpServletRequest request,Model model){
		return "/back/category/categoryPage";
	}
	/**
	 * 分页查询类别列表
	 */
	@RequestMapping("/findcategoryList")
	public String findcategoryList(HttpServletRequest request,Model model,Integer pageNum,Category category){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", categoryService.selectListByPage(category, pageNum, 15));
		return "/back/category/categoryPageList";
	}
	/**
	 * 类别新增页面
	 */
	@RequestMapping("/categoryAddPage")
	public String categoryAddPage(HttpServletRequest request,Model model){
		return "/back/category/categoryAddPage";
	}
	/**
	 * 新增类别页面提交
	 */
	@ResponseBody
	@RequestMapping("/addcategory")
	public String addcategory(HttpServletRequest request,Model model,Category category){
		category.setUseraccount(UserUtil.getCurrentUser(request).getAccountname()); 
		category.setCreatetime(new Date());
		category.setStatus(0);
		categoryService.insert(category);
		
		//更新页面左侧列表
		pageUtil.headPageListInitCategory();
		return "success";
	}
	/**
	 * 编辑类别信息页面
	 */
	@RequestMapping("/categoryUpdatePage")
	public String categoryUpdatePage(HttpServletRequest request,Model model,Long id){
		if(null==id) return "/category/categoryAddPage";
		Category category = categoryService.selectByPrimaryKey(id);
		request.setAttribute("category", category);
		return "/back/category/categoryUpdatePage";
	}
	/**
	 * 编辑类别页面提交
	 */
	@ResponseBody
	@RequestMapping("/updatecategory")
	public String updatecategory(HttpServletRequest request,Model model,Category category){
		if(null==category.getId()) return "fail";
		categoryService.updateByPrimaryKeySelective(category);
		
		//更新页面左侧列表
		pageUtil.headPageListInitCategory();
		return "success";
	}
	/**
	 * 根据id删除类别
	 */
	@ResponseBody
	@RequestMapping("/deletecategoryById")
	public String deletecategoryById(HttpServletRequest request,Model model,Long id){
		if(null==id) return "fail";
		categoryService.deletecategoryById(id);
		
		//更新页面左侧列表
		pageUtil.headPageListInitCategory();
		return "success";
	}
}
